<?php
include_once 'classes/model/Question.php';
include_once 'classes/model/Answer.php';
include_once 'classes/DBconnect.php';

/**
 * Class mapping data of DB 
 */
class DaoQuestion
{
	/**
 	 * Return result query DB MySQL
 	 */
	public function getData()
	{
		$DBconnect = DBconnect::getInstance();
		$queryResult= $DBconnect->query("SELECT vorpos.id,vorpos.textvopros,vorpos.checkorradio,otvet.id,"
										."otvet.vopridindex,otvet.textotwet,otvet.yesno FROM vorpos INNER JOIN"
										." otvet ON vorpos.id = otvet.vopridindex");
		return $this->sorts($queryResult);
	}
	
	/**
	 * Mapping data
	 * 
	 * @tableres result query of  DB
	 * 
	 * @return array text of the question and answer options to this question
	 */
	public function sorts($tableres) 
	{
		$ids=0; 
		$idquestion=0;
		$listquestion = array();
		while(list($id, $textwop, $checkorradio, $idotvet, $idid, $textvar, $yn) 
					= mysql_fetch_row($tableres))
		{
			for ($i = 0; $i < count($listquestion); $i++) 
			{
				if ($id == $listquestion[$i]->getId())
				{
					$varotvettemp = new Answer();
      				$varotvettemp->setText($textvar);
      				$varotvettemp->setIsCorrect($yn);
      				$listquestion[$i]->addAnswer($varotvettemp);
      			}
      		}
			if ($id !=$idquestion)
			{
      			$varotvettemp = new Answer(); 
      			$varotvettemp->setText($textvar);
      			$varotvettemp->setIsCorrect($yn);
      			$varvoprostemp = new Question();
      			$varvoprostemp->setText($textwop);
      			$varvoprostemp->setId($id);
      			$varvoprostemp->setType($checkorradio);
      			$varvoprostemp->addAnswer($varotvettemp);
      			$listquestion[]=$varvoprostemp;
      		}
			$idquestion=$id;		
		};
		return $listquestion;
    }
}